package models

import (
	"context"
	"go-chain-api/global"
	"go.mongodb.org/mongo-driver/bson"
)

type ListenSwapAddressConfig struct {
	Address       string `bson:"address" json:"address"`
	TokenASymbol  string `bson:"tokenASymbol" json:"tokenASymbol"`
	TokenBSymbol  string `bson:"tokenBSymbol" json:"tokenBSymbol"`
	TokenADecimal uint   `bson:"tokenADecimal" json:"tokenADecimal"`
	TokenBDecimal uint   `bson:"tokenBDecimal" json:"tokenBDecimal"`
	TokenA        string `bson:"tokenA" json:"tokenA"`
	TokenB        string `bson:"tokenB" json:"tokenB"`
	ChainName     string `bson:"chainName" json:"chainName"`
	ChainId       uint   `bson:"chainId" json:"chainId"`
}

func (l ListenSwapAddressConfig) List() ([]ListenSwapAddressConfig, error) {
	var list []ListenSwapAddressConfig
	listenClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("listen_swap_address_config")
	find, err := listenClient.Find(context.Background(), bson.M{"chainName": l.ChainName, "chainId": l.ChainId})
	if err != nil {
		return nil, err
	}
	err = find.All(context.Background(), &list)
	if err != nil {
		return nil, err
	}
	return list, nil
}

func (l ListenSwapAddressConfig) GetInfo() (ListenSwapAddressConfig, error) {
	var info ListenSwapAddressConfig
	listenClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("listen_swap_address_config")
	err := listenClient.FindOne(context.Background(), bson.M{"address": l.Address}).Decode(&info)
	return info, err
}
